package dao

import (
	"zg3yk/inits"
	"zg3yk/model"
)

func AddArticle(title string, context string, userId int) (err error) {
	result := &model.Article{
		UserId:   userId,
		Title:    title,
		Contexts: context,
	}
	if err = inits.DB.Create(&result).Error; err != nil {
		return err
	}
	return nil
}
func ListArticle() (result []*model.Article, err error) {
	if err = inits.DB.Order("like_count desc").Find(&result).Error; err != nil {
		return nil, err
	}
	return result, nil
}
func GetArticle(userId int) (list []*model.Article, err error) {
	var fouc *model.Fouc
	if err = inits.DB.Where("user_id=?", userId).Find(&fouc).Error; err != nil {
		panic("没有该用户关注的人")
	}
	var foucId []int
	foucId = append(foucId, fouc.FoucId)

	if err = inits.DB.Where("user_id in (?)", foucId).Find(&list).Error; err != nil {
		panic(err)
	}
	var list1 []*model.Article
	if err = inits.DB.Where("user_id not in (?)", foucId).Find(&list1).Error; err != nil {
		panic(err)
	}
	list = append(list, list1...)
	return list, nil
}
func AddArticleLike(button, articleId, userId int) (err error) {
	var a *model.Article
	var ar *model.ArticleLike
	if err = inits.DB.Where("id=?", articleId).Find(&a).Error; err != nil {
		return err
	}
	if button == 1 {
		inits.DB.Where("user_id=? AND article_id=?", userId, articleId).Find(&ar)
		if ar.ID == 0 {
			art := &model.ArticleLike{
				UserId:    userId,
				ArticleId: articleId,
			}
			inits.DB.Create(&art)
		}
		count := a.LikeCount + 1
		inits.DB.Model(&a).Update("like_count", count)
	} else if button == 2 {
		count := a.LikeCount - 1
		inits.DB.Model(&a).Update("like_count", count)
	}
	return nil
}
func AddContextLike(button, contextId, userId int) (err error) {
	var a *model.Context
	var ar *model.ContextLike
	if err = inits.DB.Where("id=?", contextId).Find(&a).Error; err != nil {
		return err
	}
	if button == 1 {
		inits.DB.Where("user_id=? AND article_id=?", userId, contextId).Find(&ar)
		if ar.ID == 0 {
			art := &model.ContextLike{
				UserId:    userId,
				ContextId: contextId,
			}
			inits.DB.Create(&art)
		}
		count := a.LikeCount + 1
		inits.DB.Model(&a).Update("like_count", count)
	} else if button == 2 {
		count := a.LikeCount - 1
		inits.DB.Model(&a).Update("like_count", count)
	}
	return nil
}
func AddContexts(context string, userId int, fatherId int, articleId int, button int) (err error) {
	var arti *model.Article
	if err = inits.DB.Where("id=?", articleId).Find(&arti).Error; err != nil {
		return err
	}
	if button == 1 {
		art := &model.Context{
			UserId:    userId,
			Context:   context,
			FatherId:  fatherId,
			ArticleId: articleId,
		}
		inits.DB.Create(&art)
		count := arti.ContextCount + 1
		inits.DB.Model(&arti).Update("context_count", count)
	} else if button == 2 {
		count := arti.ContextCount - 1
		inits.DB.Model(&arti).Update("context_count", count)
	}
	return nil
}
func AddContext(button int, context string, userId int, articleId int) (err error) {
	var arti *model.Article
	if err = inits.DB.Where("id=?", articleId).Find(&arti).Error; err != nil {
		return err
	}
	if button == 1 {
		art := &model.Context{
			UserId:    userId,
			Context:   context,
			ArticleId: articleId,
		}
		inits.DB.Create(&art)
		count := arti.ContextCount + 1
		inits.DB.Model(&arti).Update("context_count", count)
	} else if button == 2 {
		count := arti.ContextCount - 1
		inits.DB.Model(&arti).Update("context_count", count)
	}
	return nil
}
func GetArticleLike(userId int) (result []*model.ArticleLike, err error) {
	if err = inits.DB.Where("user_id=?", userId).Find(&result).Error; err != nil {
		return nil, err
	}
	return result, nil
}
func GetContextLike(userId int) (like []*model.ContextLike, err error) {

	if err = inits.DB.Where("user_id=?", userId).Find(&like).Error; err != nil {
		return nil, err
	}
	return like, nil
}
